import 'package:flutter/material.dart';

class AnimationImg extends StatefulWidget {
  final int counts; //图片数量
  final double? imgWidth;
  final double? imgHeight;
  final String sufix;
  final String imgUrl;
  final bool isLoop;
  const AnimationImg({super.key, required this.counts, this.imgWidth, required this.sufix, required this.imgUrl, this.isLoop = true, this.imgHeight});

  @override
  State<AnimationImg> createState() => _AnimationImgState();
}

class _AnimationImgState extends State<AnimationImg> with TickerProviderStateMixin {

  late AnimationController animationController;
  late Animation<int> animatable;
  String imgIndexStr = '0';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    animationController = AnimationController(duration: const Duration(seconds: 4), vsync: this);
    animationController.repeat();
    animatable = IntTween(begin: 0,end: widget.counts).animate(animationController);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    animationController.stop();
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animatable,
      builder: (ctx,child){
        int imgIndex = animatable.value;
        if(imgIndex < 10){
          imgIndexStr = '00$imgIndex';
        }else if(imgIndex < 100){
          imgIndexStr = '0$imgIndex';
        }else{
          imgIndexStr = '$imgIndex';
        }
        return widget.imgWidth != null?
        Image.asset(widget.isLoop? '${widget.sufix}$imgIndexStr.png':widget.imgUrl,width: widget.imgWidth,fit: BoxFit.fitWidth,gaplessPlayback:true)
        :
        Image.asset(widget.isLoop? '${widget.sufix}$imgIndexStr.png':widget.imgUrl,height: widget.imgHeight,fit: BoxFit.fitHeight,gaplessPlayback:true);

      },
    );
  }
}
